package com.solo.controller;

import com.solo.entity.User;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/*
 * FileName: HomeController.java
 * Description: 处理首页访问
 * Author:   woolflighthouse@163.com
 * Date:     2025-11-23 7:05
 */
@Controller
public class HomeController {
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * 访问首页
     */
    @GetMapping("/home")
    public String toHome(HttpSession session, Model model) {
        // 从session中获取登录用户信息
        User loginUser = (User) session.getAttribute("loginUser");

        if (loginUser == null) {
            logger.debug("未登录用户尝试访问首页，跳转到登录页");
            return "redirect:/login";
        }

        // 已登录，将用户信息传递到视图
        model.addAttribute("user", loginUser);
        logger.debug("用户访问首页: {}", loginUser.getUsername());
        return "home";
    }
}
